//
// Created by none98 on 2021/9/23.
//

#include <Context.h>
#include <spdlog/spdlog.h>
#include "MoveService.h"
#include "Move.pb.h"
#include "GameServer.h"
#include "PlayerBin.h"

void MoveService::Serve(TcpConnectionPtr conn, std::string message) {
    MoveRequest req;
    MoveResponse res;
    std::string response;
    req.ParseFromString(message);
    Context* context = static_cast<Context*>(conn->GetContext());
    if(context->GetUid()!=req.token()) {
        spdlog::info("token authority failed in MoveReq");
        res.set_status(GameServer::StatusCode::authFailed);
    }else{
        context->GetPlayerBin()->GetPosition()->Set(req.x(),req.y(),req.z());
        //set res
        res.set_status(GameServer::StatusCode::success);
    }

    std::string proto =std::move(res.SerializeAsString());
    int length = htonl(8+proto.size());
    int type = htonl(GameServer::ProtoType::moveRes);
    response.append(reinterpret_cast<char*>(&length),4);
    response.append(reinterpret_cast<char*>(&type),4);
    response.append(proto);
    conn->Send(response);
}